﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OntologyCache;
using biolit_config;

namespace UCSD_BioLit
{
    public partial class MarkupAliasing : Form
    {
        private OntologyNode on;        

        public OntologyNode SelectedNode
        {
            get { return on; }
            
        }


        private bool MarkAll;

        public bool ApplyMarkupToAll
        {
            get { return MarkAll; }
            
        }

        private bool persistTag;
        
        public bool PersistTag
        {
            get { return persistTag; }
        }

        public MarkupAliasing()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (OntologyCache.OntologyInfo OntInfo in Globals.ThisAddIn.m_Ontologies)
            {
                if (OntInfo.OntologyName == (string)this.cbOntologies.SelectedItem)
                {
                    this.tvOntologyTerms.Nodes.Clear();
                    foreach (var Item in OntInfo.m_OntologyTree)
                    {
                        OntologyCache.OntologyNode on = (OntologyCache.OntologyNode)Item.Value;
                        if (on.IsTopLevelNode == true)
                        {
                            TreeNode tn = tvOntologyTerms.Nodes.Add(on.Name);
                            tn.Tag = on;
                            if (on.Children.Count > 0)
                            {
                                RecursiveAdd(on, tn);
                            }
                        }
                    }
                }
            }
        }

        private void /*TreeNode*/ RecursiveAdd(OntologyCache.OntologyNode on, TreeNode tn)
        {
            if (on.Children.Count == 0)
            {
                return;

            }
            foreach (OntologyNode child in on.Children)
            {
                TreeNode tCur = tn.Nodes.Add(child.Name);
                tCur.Tag = child;
                RecursiveAdd(child, tCur);

            }
            //return tCur;
        }
        
        private void OnDropDown(object sender, EventArgs e)
        {
            this.cbOntologies.Items.Clear();
            foreach (OntList ontology in Globals.ThisAddIn.m_SelectedOntologies)
            {
                this.cbOntologies.Items.Add(ontology.Name);
            }
        
        }

        private void OnNodeSelected(object sender, TreeNodeMouseClickEventArgs e)
        {
            //OntologyNode on = (OntologyNode)e.Node.Tag;
            //if (on != null)
            //{
            //    txtBoxDescription.Text = on.Definition;
            //}
        }

        private void OnNodeSelected(object sender, TreeViewEventArgs e)
        {
            OntologyNode on = (OntologyNode)e.Node.Tag;
            if (on != null)
            {
                txtBoxDescription.Text = on.Definition;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            on = new OntologyNode();
            this.DialogResult = DialogResult.OK;
            if (this.tvOntologyTerms.SelectedNode != null)
            {
                OntologyNode onSel = (OntologyNode)this.tvOntologyTerms.SelectedNode.Tag;
                on.id = onSel.id;
                on.OntologyName = onSel.OntologyName;
                on.Definition = onSel.OntologyName;
                on.Name = onSel.Name;

                
                MarkAll = this.cbAll.Checked;
                persistTag = this.cbAlias.Checked;
            }
            
            this.Close();
        }

        private void MarkupAliasing_Load(object sender, EventArgs e)
        {
            this.cbAlias.Checked = true;
        }
    }
}
